package me.yobol.blog.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.yobol.blog.bean.UserBean;
import me.yobol.blog.commons.LoginStatus;
import me.yobol.blog.exception.PasswordNotTrueException;
import me.yobol.blog.exception.UserNameNotFoundException;
import me.yobol.blog.service.UserService;

public class LoginInServlet extends HttpServlet {

	private static final long serialVersionUID = -3145829253452010067L;

	/**
	 * Constructor of the object.
	 */
	public LoginInServlet() {
		super();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String uName = request.getParameter("uName");
		String uPwd = request.getParameter("uPwd");
		
		//字符串变量returnUrl是用户在访问login.jsp页面之前访问的页面
		String returnUrl = request.getParameter("returnUrl").split(request.getContextPath())[1];
		
		UserBean user = new UserBean();
		user.setName(uName);
		user.setPassword(uPwd);
		
		String forwardUrl = null;
		
		try {
			if(UserService.loginIn(user)){
				//设置登录状态
				request.getSession().setAttribute("LoginStatus", LoginStatus.USER);
				request.getSession().setAttribute("Name", uName);
				
				if(returnUrl == null || returnUrl.endsWith("register.jsp") || returnUrl.endsWith("login.jsp")){
					forwardUrl = "/index.jsp";
				}else{
					forwardUrl = returnUrl;
				}
			}else{//else分支并不会被执行到
				//设置登录状态
				request.getSession().setAttribute("LoginStatus", LoginStatus.ANONYMOUS);

				forwardUrl = "/pages/login.jsp";
			}
		} catch (UserNameNotFoundException e) {
			System.out.println(e.toString());
			request.getSession().setAttribute("LoginStatus", LoginStatus.ANONYMOUS);
			request.setAttribute("msg", e.toString());
			forwardUrl = "/pages/login.jsp";
		} catch (PasswordNotTrueException e){
			System.out.println(e.toString());
			request.getSession().setAttribute("LoginStatus", LoginStatus.ANONYMOUS);
			request.setAttribute("msg", e.toString());
			forwardUrl = "/pages/login.jsp";
		}
		
		RequestDispatcher rd = request.getRequestDispatcher(forwardUrl);
		rd.forward(request, response);
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

}
